/* 201812-4	数据中心
用链式前向星保存图，用DijkstraSP找“最短路”。
但松弛所用的权是路径最大边长而不是总边长。
即timeTo[v] = max(timeTo[u], u~v.t)
与C++版相比，2倍时间、10倍空间。
*/
import java.util.*;

public class Main{
  public static void main(String[] args){
    Scanner s = new Scanner(System.in);
    final int n = s.nextInt(), m = s.nextInt(), r = s.nextInt();
    int[] head = new int[n + 1];
    int[] time = new int[m * 2 + 1];
    int[] to   = new int[m * 2 + 1];
    int[] next = new int[m * 2 + 1];
    for(int e = 1, m2 = m * 2, u, v, t; e <= m2;){
      u = s.nextInt();
      v = s.nextInt();
      t = s.nextInt();
      time[e] = t; to[e] = v; next[e] = head[u]; head[u] = e++;
      time[e] = t; to[e] = u; next[e] = head[v]; head[v] = e++;
    }
    ArrayDeque<Integer> q = new ArrayDeque<Integer>();
    q.add(r);
    boolean[] inQ = new boolean[n + 1];
    inQ[r] = true;
    int[] timeTo = new int[n + 1];
    Arrays.fill(timeTo, Integer.MAX_VALUE);
    timeTo[r] = 0;
    while(!q.isEmpty()){
      int u = q.poll();
      inQ[u] = false;
      for(int e = head[u]; e > 0; e = next[e]){ // SPFA
        int v = to[e];
        int t = Math.max(timeTo[u], time[e]);
        if(timeTo[v] > t){ // relax
          timeTo[v] = t;
          if(!inQ[v]){
            q.add(v);
            inQ[v] = true;
          }
        }
      }
    }
    int ans = 0;
    for(int i = 1; i <= n; ++i){
      if(ans < timeTo[i]){
        ans = timeTo[i];
      }
    }
    System.out.println(ans);
  }
}